package com.javaspeak.algorithm.bubblesort.notrecursive;

import java.io.StringWriter;

/**
 * @author John Dickerson
 */
public class BubbleSort {

    public static void showArray( String[] names, int counter ){

        StringWriter sw = new StringWriter();

        for ( int i=0; i<names.length; i++ ){

            if ( i == counter -1 ){

                sw.write( "<" + names[ i ] + " " );
            }
            else if ( i == counter ){

                sw.write( names[ i ] + "> " );
            }
            else {

                sw.write( names[ i ] + " " );
            }
        }

        System.out.println( sw.toString() );
    }


    public static void bubbleSort( String[] names ){

        boolean swapped = true;

        while( swapped == true ){

            String swap;
            swapped = false;

            for ( int i=1; i<names.length; i++ ){

                showArray( names, i );

                if ( names[ i - 1 ].compareTo( names[ i ] ) > 0 ){

                    swap = names[ i - 1];
                    names[ i - 1 ] =  names[ i ];
                    names[ i ] = swap;
                    swapped = true;
                }
            }
        }
    }
}
